package edu.pku.sei.modeler.uml.commands.usecase;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.uml.UseCases.Extend;
import edu.pku.sei.modeler.model.uml.UseCases.UseCase;
import edu.pku.sei.modeler.uml.commands.ConnectionModelDeleteCommand;

public class UMLExtendDeleteCommand extends ConnectionModelDeleteCommand {

	public UMLExtendDeleteCommand(MetaModelElement connectionModel,
			MetaModelElement sourceModel, MetaModelElement targetModel) {
		super(connectionModel, sourceModel, targetModel);
	}

	@Override
	public boolean canExecute() {
		MetaModelElement sourceModelElement = this.sourceModel;
		MetaModelElement targetModelElement = this.targetModel;

		if (sourceModelElement instanceof UseCase
				&& targetModelElement instanceof UseCase) {
			return true;
		}

		return false;
	}

	@Override
	public void execute() {
		UseCase sourceUseCase = (UseCase) sourceModel;
		Extend extendLink = (Extend) connectionModel;
		sourceUseCase.getExtend().remove(extendLink);
	}

	@Override
	public void undo() {
		UseCase sourceUseCase = (UseCase) sourceModel;
		Extend extendLink = (Extend) connectionModel;
		sourceUseCase.getExtend().add(extendLink);
		extendLink.setContainer(sourceUseCase);
		extendLink.setExtendedCase((UseCase) targetModel);
		extendLink.setModel(sourceUseCase.getModel());
	}

}
